import 'dart:io';

import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yp/config/index.dart';
import 'package:yp/util/util.dart';
import 'package:yp/widgets/button_widget.dart';

import '../../router.dart';
import 'logic.dart';
import 'state.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final LoginLogic logic = Get.put(LoginLogic());
  final LoginState state = Get
      .find<LoginLogic>()
      .state;

  //750.w
  // Row > Container + Text + Cingtainer

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.white,
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        iconTheme: IconThemeData(color: Colors.black),
        elevation: 0,
        title: Text(
          "登录",
          style: TextStyle(
            color: Colors.black,
            fontSize: 34.sp,
          ),
        ),
      ),
      body: GetBuilder<LoginLogic>(
        builder: (logic) {
          return Stack(
            children: [
              Column(
                children: [
                  SizedBox(height: 60.w),
                  Image(
                      image: AssetImage("assets/images/logo.png"), width: 80.0),
                  Padding(padding: EdgeInsets.only(top: 15.w, bottom: 15.w)),
                  Text(
                    "壹品慧",
                    textAlign: TextAlign.center,
                  ),
                  Container(
                    padding: EdgeInsets.all(70.w),
                    color: Colors.white,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        state.loginType == 1
                            ? Column(
                          children: [

                            ///手机号
                            Container(
                              height: 110.w,
                              alignment: Alignment.centerLeft,
                              margin: EdgeInsets.only(bottom: 10.w),
                              child: TextField(
                                controller: state.controller1,
                                style: TextStyle(
                                  fontSize: 30.sp,
                                  height: 1.5,
                                ),
                                textAlignVertical:
                                TextAlignVertical.center,
                                maxLines: 1,
                                decoration: InputDecoration(
                                  isDense: true,
                                  contentPadding: EdgeInsets.zero,
                                  prefixIcon: Container(
                                    width: 130.w,
                                    alignment: Alignment(-0.99, 0.0),
                                    child: Row(
                                      children: [
                                        Text(
                                          "+86",
                                          style: TextStyle(
                                            color: Colors.black,
                                            fontSize: 30.sp,
                                          ),
                                        ),
                                        // 0xffeeeeee

                                        SizedBox(width: 15.w),
                                        // Padding(
                                        //   padding: EdgeInsets.only(
                                        //     left: 8.w,
                                        //     right: 8.w,
                                        //     top: 8.w,
                                        //   ),
                                        //   child: Icon(Icons
                                        //       .keyboard_arrow_right_sharp),
                                        // ),

                                        VerticalDivider(
                                          color: Colors.grey,
                                          endIndent: 30.w,
                                          indent: 30.w,
                                          width: 2.w,
                                        )
                                      ],
                                    ),
                                  ),
                                  suffixIcon: GestureDetector(
                                    onTap: () {
                                      setState(() {
                                        state.controller1.clear();
                                      });
                                    },
                                    child: Icon(Icons.close),
                                  ),
                                  prefixIconConstraints: BoxConstraints(
                                    maxWidth: 130.w,
                                    minWidth: 60.w,
                                  ),
                                  hintText: '请输入手机号',
                                  hintStyle: TextStyle(
                                    fontSize: 30.sp,
                                    color: Color(0xffbbbbbb),
                                  ),
                                  enabledBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: Color(0xffdddddd),
                                    ),
                                  ),
                                  focusedBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: EsColors.Primary,
                                    ),
                                  ),
                                  border: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: Color(0xffdddddd),
                                    ),
                                  ),
                                ),
                              ),
                            ),

                            ///验证码
                            Container(
                              height: 110.w,
                              alignment: Alignment.centerLeft,
                              margin: EdgeInsets.only(bottom: 10.w),
                              child: TextField(
                                controller: state.controller2,
                                style: TextStyle(
                                  fontSize: 30.sp,
                                  height: 1.5,
                                ),
                                textAlignVertical:
                                TextAlignVertical.center,
                                maxLines: 1,
                                // obscureText: !state.showPassword.value,
                                decoration: InputDecoration(
                                  isDense: true,
                                  contentPadding: EdgeInsets.zero,
                                  prefixIconConstraints: BoxConstraints(
                                    maxWidth: 60.w,
                                    minWidth: 60.w,
                                  ),
                                  suffixIconConstraints: BoxConstraints(
                                    maxWidth: 150.w,
                                    minWidth: 150.w,
                                  ),
                                  suffixIcon: InkWell(
                                    onTap: () {
                                      print(state.controller1.text);
                                      if (state
                                          .controller1.text.isEmpty) {
                                        toast("请输入手机号");
                                        return;
                                      }
                                      if (!RegexUtil.isMobileSimple(
                                          state.controller1.text)) {
                                        toast("手机号格式错误");
                                        return;
                                      }
                                      if (logic.state.seconds > 0 &&
                                          logic.state.seconds < 60) {
                                        return;
                                      }
                                      logic.sendSms();
                                    },
                                    child: Align(
                                      alignment: Alignment.centerRight,
                                      child: Container(
                                        width: 180.w,
                                        height: 60.w,
                                        decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(10.w)),
                                            border: Border.all(
                                                color: Color(0xffe1e1e1))

                                        ),
                                        child: Center(
                                          child: Text(
                                            logic.state.verifyStr,
                                            style: TextStyle(
                                              fontSize: 24.sp,
                                              color: Color(0xff717171),
                                              height: 1.4,
                                            ),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                      ),
                                    ),
                                  ),
                                  hintText: '输入验证码',
                                  hintStyle: TextStyle(
                                    fontSize: 30.sp,
                                    color: Color(0xffbbbbbb),
                                  ),
                                  enabledBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: Color(0xffdddddd),
                                    ),
                                  ),
                                  focusedBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: EsColors.Primary,
                                    ),
                                  ),
                                  border: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: Color(0xffdddddd),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        )
                            :
                        //  账号密码登录
                        Column(
                          children: [

                            ///账号
                            Container(
                              height: 110.w,
                              alignment: Alignment.centerLeft,
                              margin: EdgeInsets.only(bottom: 10.w),
                              child: TextField(
                                controller: state.controller1,
                                style: TextStyle(
                                  fontSize: 30.sp,
                                  height: 1.5,
                                ),
                                textAlignVertical:
                                TextAlignVertical.center,
                                maxLines: 1,
                                decoration: InputDecoration(
                                  isDense: true,
                                  contentPadding: EdgeInsets.zero,
                                  suffixIcon: GestureDetector(
                                    onTap: () {
                                      setState(() {
                                        state.controller1.clear();
                                      });
                                    },
                                    child: Icon(Icons.close),
                                  ),
                                  prefixIconConstraints: BoxConstraints(
                                    maxWidth: 60.w,
                                    minWidth: 60.w,
                                  ),
                                  hintText: '请输入手机号',
                                  hintStyle: TextStyle(
                                    fontSize: 30.sp,
                                    color: Color(0xffbbbbbb),
                                  ),
                                  enabledBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: Color(0xffdddddd),
                                    ),
                                  ),
                                  focusedBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: EsColors.Primary,
                                    ),
                                  ),
                                  border: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: Color(0xffdddddd),
                                    ),
                                  ),
                                ),
                              ),
                            ),

                            ///密码
                            Container(
                              height: 110.w,
                              alignment: Alignment.centerLeft,
                              margin: EdgeInsets.only(bottom: 10.w),
                              child: TextField(
                                controller: state.controller2,
                                style: TextStyle(
                                  fontSize: 30.sp,
                                  height: 1.5,
                                ),
                                textAlignVertical:
                                TextAlignVertical.center,
                                maxLines: 1,
                                obscureText: state.show1,
                                decoration: InputDecoration(
                                  isDense: true,
                                  contentPadding: EdgeInsets.zero,
                                  suffixIcon: IconButton(
                                    icon: Icon(
                                      //根据passwordVisible状态显示不同的图标
                                      state.show1 == true
                                          ? Icons.visibility
                                          : Icons.visibility_off,
                                      color: Color(0xffcccccc),
                                    ),
                                    onPressed: () {
                                      //更新状态控制密码显示或隐藏
                                      setState(() {
                                        state.show1 = !state.show1;
                                      });
                                    },
                                  ),
                                  // suffixIcon:GestureDetector(
                                  //   onTap: () {
                                  //     setState(() {
                                  //       state.controller2.clear();
                                  //     });
                                  //   },
                                  //   child: Icon(Icons.close),
                                  // ),
                                  prefixIconConstraints: BoxConstraints(
                                    maxWidth: 60.w,
                                    minWidth: 60.w,
                                  ),
                                  hintText: '请输入密码',
                                  hintStyle: TextStyle(
                                    fontSize: 30.sp,
                                    color: Color(0xffbbbbbb),
                                  ),
                                  enabledBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: Color(0xffdddddd),
                                    ),
                                  ),
                                  focusedBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: EsColors.Primary,
                                    ),
                                  ),
                                  border: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                      color: Color(0xffdddddd),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),

                        /// email

                        Container(
                          padding: EdgeInsets.only(top: 10.w, bottom: 60.w),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Expanded(
                                child: RichText(
                                  text: TextSpan(
                                    children: [
                                      WidgetSpan(
                                        child: GestureDetector(
                                          onTap: () {
                                            logic.selectStatus();
                                          },
                                          child: state.status
                                              ? Padding(
                                            padding: EdgeInsets.only(
                                                right: 10.w, bottom: 3.w),
                                            child: Image(
                                              image: AssetImage(
                                                  "assets/images/selectIcon1.png"),
                                              width: 26.w,
                                              height: 26.w,
                                            ),
                                          )
                                              : Padding(
                                            padding: EdgeInsets.only(
                                                right: 10.w, bottom: 3.w),
                                            child: Image(
                                              image: AssetImage(
                                                  "assets/images/save.png"),
                                              width: 26.w,
                                              height: 26.w,
                                            ),
                                          ),
                                        ),
                                      ),
                                      TextSpan(
                                        text: ' 同意',
                                        style: TextStyle(
                                            color: Colors.black,
                                            fontSize: 24.w),
                                      ),
                                      TextSpan(
                                        text: '《服务协议》',
                                        style: TextStyle(
                                          color: Colors.red,
                                          fontSize: 12,
                                        ),
                                        recognizer: TapGestureRecognizer()
                                          ..onTap = () {
                                            // Get.toNamed(RouteConfig.webview,
                                            //     parameters: {
                                            //       "url":
                                            //       "https://img.zrhsh.com/zrhshgg/treaty/fwxy.html",
                                            //     });
                                          },
                                      ),

                                      TextSpan(
                                        text: '《隐私协议》',
                                        style: TextStyle(
                                          color: Colors.red,
                                          fontSize: 12,
                                        ),
                                        recognizer: TapGestureRecognizer()
                                          ..onTap = () {
                                            // Get.toNamed(RouteConfig.webview,
                                            //     parameters: {
                                            //       "url":
                                            //       "https://img.zrhsh.com/zrhshgg/treaty/yszc-hxk.html",
                                            //     });
                                          },
                                      ),
                                    ],
                                  ),
                                ),
                              ),

                            ],
                          ),
                        ),
                        ButtonWidget(
                          width: 600.w,
                          title: "登录",
                          bgColor:
                          state.status ? Colors.red : Color(0xffcccccc),
                          onTap: () {
                            if (state.status) {
                              logic.onSubmit();
                            }
                          },
                        ),

                        SizedBox(height: 60.w),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            GestureDetector(
                              onTap: () {
                                // state.controller1.text = "";
                                state.controller2.text = "";
                                if (state.loginType == 1) {
                                  state.loginType = 2;
                                } else {
                                  state.loginType = 1;
                                }
                                logic.update();
                              },
                              child: Text(
                                state.loginType == 1 ? "账号密码登录" : "验证码登录",
                                style: TextStyle(
                                    color: Color(0xff666666), fontSize: 28.w),
                              ),
                            ),
                            if( state.loginType != 1 ) Container(
                              width: 2.w,
                              height: 22.w,
                              color: Color(0xff666666),
                              margin: EdgeInsets.symmetric(horizontal: 25.w),
                            ),
                            if( state.loginType != 1 )GestureDetector(
                              onTap: () {
                                // Get.toNamed(RouteConfig.resetPassword);
                              },
                              child: Text(
                                "忘记密码",
                                style: TextStyle(
                                    color: Color(0xff666666), fontSize: 28.w),
                              ),
                            )
                          ],
                        ),
                        Container(
                            padding: EdgeInsets.only(top: 30.w),
                            child: Center(
                              child: Text(
                                  "版本：${logic.state.version} ${Config.env == 1 ? '(${logic.state.buildNumber})（Beta）' : Config.env == 2 ? '(${logic.state.buildNumber})（UAT）' : Config.env == 4 ? '(${logic.state.buildNumber})（Test）' : ''}",
                                  style: TextStyle(color: Colors.black54)),
                            )),
                        // Container(
                        //   padding: EdgeInsets.only(top: 40.w),
                        //   child: Center(
                        //     child: Text(
                        //       "本机号码一键登录",
                        //       textAlign: TextAlign.center,
                        //       style: TextStyle(
                        //         color: Colors.black,
                        //       ),
                        //     ),
                        //   ),
                        // ),
                      ],
                    ),
                  ),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.only(bottom: 30.w),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Container(
                            height: 100.w,
                            width: 750.w,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [
                                // GestureDetector(
                                //   onTap: () {
                                //     if (state.status) {
                                //       logic.wxLogin();
                                //     } else {
                                //       toast('请先阅读并同意勾选用户协议');
                                //       return;
                                //     }
                                //   },
                                //   child: Image(
                                //     image:
                                //         AssetImage("assets/images/wxIcon.png"),
                                //     width: 64.w,
                                //     height: 64.w,
                                //   ),
                                // ),
                                // GestureDetector(
                                //   onTap: () {},
                                //   child: Image(
                                //     image: AssetImage("assets/images/zfbIcon.png"),
                                //     width: 64.w,
                                //     height: 64.w,
                                //   ),
                                // ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
              state.show == true
                  ? GetBuilder<LoginLogic>(
                builder: (logic) {
                  return Positioned(
                      child: Container(
                        color: Colors.black12,
                        child: SimpleDialog(
                          title: Text(
                            '个人信息保护',
                            style: TextStyle(
                              color: Color(0xff030303),
                              fontSize: 28.w,
                              fontWeight: FontWeight.w600,
                            ),
                            textAlign: TextAlign.center,
                          ),
                          titleTextStyle: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                              color: Colors.blue),
                          children: [
                            Column(
                              children: [
                                Container(
                                  padding: EdgeInsets.symmetric(
                                    horizontal: 30.w,
                                  ),
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(100)),
                                      color: Colors.white),
                                  child: Column(
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: [
                                      Text('感谢您对慧生活一直以来的信任！'),
                                      SizedBox(
                                        height: 20.w,
                                      ),
                                      Container(
                                        child: RichText(
                                          text: TextSpan(
                                            children: [
                                              TextSpan(
                                                text: '我们依据最新的监管要求更新了',
                                                style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize: 22.w,
                                                ),
                                              ),
                                              TextSpan(
                                                text: '《壹品慧生活隐私权政策》',
                                                style: TextStyle(
                                                  color: Color(0xffEA5504),
                                                  fontSize: 22.w,
                                                ),
                                                recognizer:
                                                TapGestureRecognizer()
                                                  ..onTap = () {
                                                    // Get.toNamed(
                                                    //     RouteConfig
                                                    //         .webview,
                                                    //     parameters: {
                                                    //       "url":
                                                    //       "https://img.zrhsh.com/zrhshgg/treaty/yszc-hxk.html",
                                                    //     });
                                                  },
                                              ),
                                              TextSpan(
                                                text: '和',
                                                style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize: 22.w,
                                                ),
                                              ),
                                              TextSpan(
                                                text: '《壹品慧生活服务协议》',
                                                style: TextStyle(
                                                  color: Color(0xffEA5504),
                                                  fontSize: 22.w,
                                                ),
                                                recognizer:
                                                TapGestureRecognizer()
                                                  ..onTap = () {
                                                    // Get.toNamed(
                                                    //     RouteConfig
                                                    //         .webview,
                                                    //     parameters: {
                                                    //       "url":
                                                    //       "https://img.zrhsh.com/zrhshgg/treaty/fwxy.html",
                                                    //     });
                                                  },
                                              ),
                                              TextSpan(
                                                text:
                                                '（点击了解更新后的详细内容），特向您说明如下：',
                                                style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize: 22.w,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      ),
                                      Text(
                                        '1、为向您提供缴费相关基本功能，我们会收集、使用必要的信息；',
                                        style: TextStyle(
                                          color: Color(0xff999999),
                                          fontSize: 22.w,
                                        ),
                                      ),
                                      SizedBox(
                                        height: 20.w,
                                      ),
                                      Text(
                                        '2、为保障您的账号与使用安全，您需要授权我们读取本机识别码，您有权拒绝或取消授权，取消后不会影响您使用我们提供的其他服务；',
                                        style: TextStyle(
                                          color: Color(0xff999999),
                                          fontSize: 22.w,
                                        ),
                                      ),
                                      SizedBox(
                                        height: 20.w,
                                      ),
                                      Text(
                                        '3、为在慧生活APP首页向您展示所在位置附近营业厅信息，您需要授权我们获取您的位置权限，您有权拒绝或取消授权，取消后不会影响您使用我们提供的其他服务；',
                                        style: TextStyle(
                                          color: Color(0xff999999),
                                          fontSize: 22.w,
                                        ),
                                      ),
                                      SizedBox(
                                        height: 20.w,
                                      ),
                                      Text(
                                        '4、我们会采取业界先进的安全措施保护您的信息安全；',
                                        style: TextStyle(
                                          color: Color(0xff999999),
                                          fontSize: 22.w,
                                        ),
                                      ),
                                      SizedBox(
                                        height: 20.w,
                                      ),
                                      Text(
                                        '5、未经您同意，我们不会从第三方处获取、共享或向其提供您的信息；',
                                        style: TextStyle(
                                          color: Color(0xff999999),
                                          fontSize: 22.w,
                                        ),
                                      ),
                                      SizedBox(
                                        height: 20.w,
                                      ),
                                      Text(
                                        '6、您可以查询、更正您的个人信息；',
                                        style: TextStyle(
                                          color: Color(0xff999999),
                                          fontSize: 22.w,
                                        ),
                                      ),
                                      SizedBox(
                                        height: 20.w,
                                      ),
                                      Text(
                                        '7、您如果选择不同意，则默认退出当前APP',
                                        style: TextStyle(
                                          color: Color(0xff999999),
                                          fontSize: 22.w,
                                        ),
                                      ),
                                      SizedBox(
                                        height: 40.w,
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  padding:
                                  EdgeInsets.symmetric(horizontal: 30.w),
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      GestureDetector(
                                        onTap: () {
                                          exit(0);
                                        },
                                        child: Container(
                                          width: 200.w,
                                          height: 60.w,
                                          decoration: BoxDecoration(
                                            borderRadius: BorderRadius.all(
                                              Radius.circular(100),
                                            ),
                                            border: Border.all(
                                                color: Color(0xff666666)),
                                          ),
                                          child: Center(
                                            child: Text(
                                              "不同意",
                                              style: TextStyle(
                                                fontSize: 24.w,
                                                color: Color(0xff666666),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                      GestureDetector(
                                        onTap: () {
                                          logic.hide();
                                        },
                                        child: Container(
                                          width: 200.w,
                                          height: 60.w,
                                          decoration: BoxDecoration(
                                            color: Color(0xffEA5504),
                                            borderRadius: BorderRadius.all(
                                              Radius.circular(100),
                                            ),
                                            border: Border.all(
                                                color: Color(0xffEA5504)),
                                          ),
                                          child: Center(
                                            child: Text(
                                              "同意",
                                              style: TextStyle(
                                                fontSize: 24.w,
                                                color: Colors.white,
                                              ),
                                            ),
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                )
                              ],
                            )
                          ],
                          backgroundColor: Colors.white,
                          elevation: 10,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(20),
                          ),
                        ),
                      ));
                },
              )
                  : Container()
            ],
          );
        },
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<LoginLogic>();
    super.dispose();
  }
}
